package cn.lijiajia3515.cairo.auth.starter.autoconfigure;

import cn.lijiajia3515.cairo.auth.CairoAuthProperties;
import cn.lijiajia3515.cairo.auth.modules.AuthenticationClient;
import cn.lijiajia3515.cairo.auth.security.oauth2.resource.server.authentication.CairoJwtAuthenticationConverter;
import cn.lijiajia3515.cairo.starter.autoconfigure.security.oauth2.resourceserver.CairoOAuth2ResourceServerAutoConfiguration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.oauth2.jwt.Jwt;

@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(CairoOAuth2ResourceServerAutoConfiguration.class)
public class CairoAuthAutoConfiguration {

	@Bean
	@ConfigurationProperties("cairo.auth")
	public CairoAuthProperties cairoAuthProperties() {
		return new CairoAuthProperties();
	}

	@Bean
	public Converter<Jwt, AbstractAuthenticationToken> cairoJwtAuthenticationConverter(ObjectProvider<AuthenticationClient> authenticationClient) {
		return new CairoJwtAuthenticationConverter(authenticationClient.getIfAvailable());
	}


}
